// This code is released under the BSD license.
namespace PugLib
{
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Helper class that provides methods for helping unit test development, like handling
    /// expected exceptions and serialization of classes.
    /// </summary>
    public static class UnitTestHelper
    {
        #region Delegates

        public delegate void ExceptionTest();

        #endregion

        /// <summary>
        /// Tests method for an expected exception being thrown.
        /// </summary>
        /// <param name="del"></param>
        /// <param name="exception"></param>
        public static void TestException(ExceptionTest del, Type exception)
        {
            try
            {
                // Call the delegate, fail the test if it doesn't throw an exception.
                del();
                Assert.Fail("Test should have thrown exception " + exception + ".");
            }
            catch (AssertFailedException)
            {
                // If it's a failed assert, then just rethrow because it's our failure above.
                throw;
            }
            catch (Exception e)
            {
                // Test that the method threw the exception that we were expecting.
                Assert.IsInstanceOfType(e, exception, "Exception is not of the expected type.", exception.ToString());
            }
        }
    }
}